package burp;

import help.Myhelp;

import java.io.PrintWriter;

/**
 * Created by shuaidong on 2017/6/25.
 */
public class InsertionPoint implements IScannerInsertionPoint {
    private IExtensionHelpers helpers;
    private PrintWriter stdout;
    private String baseName;
    private String baseValue;
    private IHttpRequestResponse baseRequestResponse;
    private byte iparameterType;

    public InsertionPoint(IExtensionHelpers helpers, PrintWriter stdout, String baseName, String baseValue, IHttpRequestResponse baseRequestResponse, byte iparameterType) {
        this.helpers = helpers;
        this.stdout = stdout;
        this.baseName = baseName;
        this.baseValue = baseValue;
        this.baseRequestResponse = baseRequestResponse;
        this.iparameterType = iparameterType;
    }

    public String getInsertionPointName() {
        return this.baseName;
    }

    public String getBaseValue() {
        return this.baseValue;
    }

    public byte[] buildRequest(byte[] payload) {

        IRequestInfo requestInfo = helpers.analyzeRequest(baseRequestResponse.getRequest());
       /* this.stdout.println("pppppppppppppppayload:");*/

        String payloadPlain = helpers.bytesToString(payload);
        //根据content-type  //根据插入点是否是get请求，如果是get请求payload进行urlencode
        //根据参数类型来进行encodepayload
      //  byte contenttype = requestInfo.getContentType();
    /*    this.stdout.println("iparameterType:"+this.iparameterType);*/
        String insertPayload=baseValue;

        switch (this.iparameterType){
            case IParameter.PARAM_URL:
                payloadPlain =  this.helpers.urlEncode(payloadPlain);
                 insertPayload = insertPayload + payloadPlain;
                return helpers.updateParameter(baseRequestResponse.getRequest(),helpers.buildParameter(baseName,insertPayload,iparameterType));
            case IParameter.PARAM_BODY:

                insertPayload = insertPayload + payloadPlain;
                return helpers.updateParameter(baseRequestResponse.getRequest(),helpers.buildParameter(baseName,insertPayload,iparameterType));
            case IParameter.PARAM_JSON:
                insertPayload = insertPayload + payloadPlain;
                Myhelp myhelp = new Myhelp(this.helpers,this.stdout);
                try {
                   return myhelp.updateParameterJsonValue(baseRequestResponse.getRequest(),helpers.buildParameter(baseName,insertPayload,iparameterType),iparameterType);
                } catch (Exception e) {
                    this.stdout.println("body jsonformat is error");
                }
            case IParameter.PARAM_COOKIE:
                insertPayload = insertPayload + payloadPlain;
                return helpers.updateParameter(baseRequestResponse.getRequest(),helpers.buildParameter(baseName,insertPayload,iparameterType));
            default:
                insertPayload = insertPayload + payloadPlain;
                return helpers.updateParameter(baseRequestResponse.getRequest(),helpers.buildParameter(baseName,insertPayload,iparameterType));
        }


    }

    public int[] getPayloadOffsets(byte[] payload) {

        return new int[0];
    }

    public byte getInsertionPointType() {
        return iparameterType;
    }
}
